package com.exam.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.exam.bean.PaperContentType;
import com.exam.service.PaperContentTypeService;
import com.exam.service.impl.PaperContentTypeServiceImpl;
import com.exam.util.Page;
import com.exam.util.PageResult;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 内容分类Action 2012-1-3
 * 
 * @author MuLing
 * 
 */
public class PaperContentTypeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private PaperContentType parentPaperContentType = new PaperContentType();
	private PaperContentType paperContentType = new PaperContentType();
	private List<PaperContentType> pctList = new ArrayList<PaperContentType>();
	private Page page = new Page();

	// 为分页处理服务的方法
	@SuppressWarnings("unchecked")
	public void listForContentTypePageService(PaperContentTypeService s,
			String strWhere) throws Exception {
		PageResult pageResult = null;
		getPage().setEveryPage(10);
		if (strWhere != null && !"".equals(strWhere)) {
			pageResult = s.findListByPage(strWhere, getPage());// 按条件查询
		} else {
			pageResult = s.findListByPage(getPage());// 简单分页
		}
		setPage(pageResult.getPage());// 获得分页信息
		setPctList(pageResult.getList());// 获得记录
	}
	//供最顶层按条件查询
	public String listByWherePage(){
		PaperContentTypeService service = new PaperContentTypeServiceImpl();
		HttpServletRequest request = ServletActionContext.getRequest();
		String nameStr = request.getParameter("name");
		
		try {
			if(nameStr!=null&&!"".equals(nameStr)){
				if(page.getCurrentPage()>1){//存在条件的时候，将分页信息设置为初始态
					page = new Page();
				}
				listForContentTypePageService(service, "name='"+nameStr+"'");
			}else{
				listForContentTypePageService(service, "pid is null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "exception";
		}
		return "listByPage";
	}
	
	// 分页查询最顶层分类信息
	public String listByPage() {
		PaperContentTypeService service = new PaperContentTypeServiceImpl();
		try {
			listForContentTypePageService(service, "pid is null");
		} catch (Exception e) {
			e.printStackTrace();
			return "exception";
		}
		return "listByPage";
	}

	// 根据id查询分类信息
	public String listById() {
		PaperContentTypeService service = new PaperContentTypeServiceImpl();
		HttpServletRequest request = ServletActionContext.getRequest();
		String strID = request.getParameter("id");
		if (strID != null) {
			try {
				setPaperContentType(service.findModel(Integer.parseInt(strID)));

				if (getPaperContentType().getPid() != null) {
					setParentPaperContentType(service
							.findModel(getPaperContentType().getPid()));
				}
			} catch (Exception e) {
				// e.printStackTrace();
				e.printStackTrace();
				return "exception";
			}
		}
		return "listById";
	}

	// 添加内容分类
	public String add() {
		PaperContentTypeService service = new PaperContentTypeServiceImpl();
		try {
			service.add(getPaperContentType());
			listForContentTypePageService(service, "pid is null");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		}
		return "listByPage";
	}

	// 编辑内容分类
	public String edit() {
		PaperContentTypeService service = new PaperContentTypeServiceImpl();
		HttpServletRequest request = ServletActionContext.getRequest();
		String strID = request.getParameter("id");
		if (strID != null) {
			try {
				setPaperContentType(service.findOnlyModel(Integer
						.parseInt(strID)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "exception";
			}
		}
		return "edit";
	}

	// 更新内容分类
	public String update() {
		PaperContentTypeService service = new PaperContentTypeServiceImpl();
		try {
			service.updateOnlyModel(getPaperContentType());
			listForContentTypePageService(service, "pid is null");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		}
		return "listByPage";
	}

	// 删除内容分类
	public String delete() {
		PaperContentTypeService service = new PaperContentTypeServiceImpl();
		HttpServletRequest request = ServletActionContext.getRequest();
		String strID = request.getParameter("id");
		if (strID != null) {
			try {
				service.delete(Integer.parseInt(strID));
				listForContentTypePageService(service, "pid is null");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "exception";
			}
		}
		return "listByPage";
	}

	//批量删除分类信息
	public String deleteList(){
		PaperContentTypeService service = new PaperContentTypeServiceImpl(); 
		HttpServletRequest request = ServletActionContext.getRequest();// 取得Request对象
		String[] checkIDs = request.getParameterValues("checkIDs");// 取得页面上需要删除的信息对应的ID
		
		List<Integer> idList = new ArrayList<Integer>();// 用于存储需要删除的信息对应的ID的集合
		if (checkIDs != null && checkIDs.length > 0) {
			for (String strID : checkIDs) {
				idList.add(Integer.parseInt(strID));// 将ID保存到List集合中
			}
		} 
		try {
			service.delete(idList);
			listForContentTypePageService(service, "pid is null");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		} 
		return "listByPage";
	}
	
	public String list() {
		PaperContentTypeService service = new PaperContentTypeServiceImpl();
		try {
			setPctList(service.findList("pid is null"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "listPaperContentType";
	}

	public PaperContentType getParentPaperContentType() {
		return parentPaperContentType;
	}

	public void setParentPaperContentType(
			PaperContentType parentPaperContentType) {
		this.parentPaperContentType = parentPaperContentType;
	}

	public PaperContentType getPaperContentType() {
		return paperContentType;
	}

	public void setPaperContentType(PaperContentType paperContentType) {
		this.paperContentType = paperContentType;
	}

	public List<PaperContentType> getPctList() {
		return pctList;
	}

	public void setPctList(List<PaperContentType> pctList) {
		this.pctList = pctList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

}
